package main

import (
	"easyjson-demo/model"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.GET("/posts", func(c *gin.Context) {
		posts := []model.Post{
			{ID: 1, Title: "post 1", ReadCount: 1},
			{ID: 2, Title: "post 2", ReadCount: 1},
			{ID: 3, Title: "post 1", ReadCount: 1},
		}

		if c.Query("easy") == "true" {
			contentType := "application/json; charset=utf-8"
			result, _ := model.Posts{Posts: posts}.MarshalJSON()
			c.Data(200, contentType, result)
		} else {
			c.JSON(200, gin.H{"posts": posts})
		}

	})

	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
